package com.example.stock.util;

import com.example.stock.vo.DataVo;

import java.util.ArrayList;
import java.util.List;

public class CalcUtil {

    private static double sumAmount(List<DataVo> list, int from, int to) {
        List<DataVo> doubles = list.subList(from, to + 1);
        double ret = 0.0;
        for (DataVo aDouble : doubles) {
            ret = ret + (aDouble.getPrice());
        }
        return  ret;
    }

    /**
     * test -- ref函数
     */
    public static List<DataVo> REF(List<DataVo> closeData,int n) {
        List<DataVo> ret = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            ret.add(closeData.get(i));
        }
        for (int i = n; i < closeData.size(); i++) {
            DataVo cur = closeData.get(i);
            DataVo vo = new DataVo(cur.getTradeTime(), closeData.get(i-n).getPrice(),cur.getTsCode());
            ret.add(vo);
        }
        return ret;
    }

    /**
     * test -- ema函数
     */
    public static List<DataVo> EMA(List<DataVo> list, int number) {
        List<DataVo> doubles = new ArrayList<>();
        Double k = 2.0 / (number + 1.0);
        // 第一天ema等于当天收盘价
        Double ema = list.get(0).getPrice();
        doubles.add(new DataVo(list.get(0).getTradeTime(),ema));
        for (int i = 1; i < list.size(); i++) {
            // 第二天以后，当天收盘 收盘价乘以系数再加上昨天EMA乘以系数-1
            double price = list.get(i).getPrice();
            ema = price * k + (ema * (1.0 - k));
            doubles.add(new DataVo(list.get(i).getTradeTime(), ema));
        }
        return doubles;
    }

    /**
     * test -- ma函数
     */
    public static List<DataVo> MA(List<DataVo> list, int number) {
        List<DataVo> doubles = new ArrayList<>();
        for (int i = 0; i < number; i++) {
            Double ma = CalcUtil.sumAmount(list, 0, i) / (i + 1);
            doubles.add(new DataVo(list.get(i).getTradeTime(),ma));
        }
        for (int i = number; i < list.size(); i++) {
            Double divide = CalcUtil.sumAmount(list, i - number + 1, i)/number;
            doubles.add(new DataVo(list.get(i).getTradeTime(), divide));
        }
        return doubles;
    }


    /**
     * test -- 相减
     */
    public static List<DataVo> _JIAN(List<DataVo> form, List<DataVo> to) {
        if (form.size() != to.size()) {
            throw new RuntimeException("data error code 1");
        }
        List<DataVo> ret = new ArrayList<>();
        for (int i = 0; i < form.size(); i++) {
            DataVo fromVo = form.get(i);
            DataVo toVo = to.get(i);
            if (!fromVo.getTradeTime().equals(toVo.getTradeTime())) {
                throw new RuntimeException("jian==>tradeTime error code 1");
            }
            DataVo vo = new DataVo(fromVo.getTradeTime(), fromVo.getPrice() - toVo.getPrice());
            ret.add(vo);
        }
        return ret;

    }

    /**
     * test -- 相乘
     */
    public static List<DataVo> _CHENG(List<DataVo> form, int number) {

        List<DataVo> ret = new ArrayList<>();
        for (int i = 0; i < form.size(); i++) {
            DataVo fromVo = form.get(i);
            DataVo vo = new DataVo(fromVo.getTradeTime(), fromVo.getPrice()*number);
            ret.add(vo);
        }
        return ret;

    }

    /**
     * test -- 相除
     */
    public static List<DataVo> _CHU(List<DataVo> form, List<DataVo> to) {
        if (form.size() != to.size()) {
            throw new RuntimeException("data error code 1");
        }
        List<DataVo> ret = new ArrayList<>();
        for (int i = 0; i < form.size(); i++) {
            DataVo fromVo = form.get(i);
            DataVo toVo = to.get(i);
            if (!fromVo.getTradeTime().equals(toVo.getTradeTime())) {
                throw new RuntimeException("chu==>tradeTime error code 1");
            }
            DataVo vo = new DataVo(fromVo.getTradeTime(), fromVo.getPrice()/toVo.getPrice());
            ret.add(vo);
        }
        return ret;
    }

    /**
     * test -- 相除
     */
    public static List<DataVo> _CHU(List<DataVo> form, Double price) {
        List<DataVo> ret = new ArrayList<>();
        for (int i = 0; i < form.size(); i++) {
            DataVo fromVo = form.get(i);
            DataVo vo = new DataVo(fromVo.getTradeTime(), fromVo.getPrice()*price);
            ret.add(vo);
        }
        return ret;
    }
}
